package com.sdgakj.utils;

import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;
/**
 * 七牛简单上传
 * @author yrj
 *
*/
public class QnFileUploadUtil {

	private static final String IMAGEPATH = PropUtil.getInstance().get("upload.image.path");
	
	private static final String ACCESS_KEY = PropUtil.getInstance().get("qiniu_access_key");
	
	private static final String SECRET_KEY = PropUtil.getInstance().get("qiniu_secret_key");
	
	private static final String SPACE_NAME_KEY = PropUtil.getInstance().get("qiniu_space_name");
	
	private static final String DOMAIN_NAME_KEY = PropUtil.getInstance().get("qiniu_domain_name");
	
	public static String uploadFile(String imageFile,String fileName)
	{
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		String upToken = auth.uploadToken(SPACE_NAME_KEY);
		UploadManager uploadManager = new UploadManager();
		InputStream stream = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String result = "";
		try{
			String path = IMAGEPATH + "my/"+ imageFile;
			java.io.File  newFile = new java.io.File(path);
			if (!newFile.isFile() || !newFile.exists()) {
				throw new FileNotFoundException("文件不存在！");
			}
			stream = new FileInputStream(newFile); 
		    byte[] byt = new byte[stream.available()];
		    stream.read(byt);
		    out.write(byt);
//		    if(fileName==null){
		    	fileName = UUID.randomUUID().toString()+imageFile.substring(imageFile.lastIndexOf("."));
//		    }
		    Response res = uploadManager.put(byt, fileName, upToken);
			if(res.isOK()){
				result = DOMAIN_NAME_KEY+"/"+fileName;
			}
		}catch(Exception e){
			if(stream!=null){
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}finally{
			if(stream!=null){
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		return result;
	}
	
	
	public static String uploadFile(MultipartFile file,String fileName){
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		String upToken = auth.uploadToken(SPACE_NAME_KEY);
		UploadManager uploadManager = new UploadManager();
		InputStream stream = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String result = "";
		try{
			if (file==null || file.isEmpty() || file.getSize()==0) {
				throw new FileNotFoundException("文件不存在！");
			}
			String imageFile = file.getOriginalFilename();
		    byte[] byt = file.getBytes();
		    out.write(byt);
	    	fileName = UUID.randomUUID().toString()+imageFile.substring(imageFile.lastIndexOf("."));
		    Response res = uploadManager.put(byt, fileName, upToken);
			if(res.isOK()){
				result = "http://"+DOMAIN_NAME_KEY+"/"+fileName;
			}
		}catch(Exception e){
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}finally{
			if(stream!=null){
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String uploadWatermarkFile(MultipartFile file, String fileName)
	{
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		String upToken = auth.uploadToken(SPACE_NAME_KEY);
		UploadManager uploadManager = new UploadManager();
		InputStream stream = null;

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String result = "";
		try{
			if (file==null || file.isEmpty() || file.getSize()==0) {
				throw new FileNotFoundException("文件不存在！");
			}
			String imageFile = file.getOriginalFilename();
			byte[] byt = file.getBytes();
			out.write(byt);
			fileName = UUID.randomUUID().toString()+imageFile.substring(imageFile.lastIndexOf("."));
			Response res = uploadManager.put(byt, fileName, upToken);
			//"|watermark/1/image/" + Base64.encode("http://7xo98k.com1.z0.glb.clouddn.com/20a7c5a1-11e1-4823-8bac-fb110b656831.png".getBytes())
			if(res.isOK()){
				result = "http://" + DOMAIN_NAME_KEY + "/" + fileName + "?imageMogr2/auto-orient/strip/quality/50";
				//            + "/dissolve"
				//            + "/100"
				//            + "/gravity"
				//            + "/SouthWest" + "/dx" + "/10" + "/dy" + "/30" + "/ws" + "/0.1";
			}
		}catch(Exception e){
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}finally{
			if(stream!=null){
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return result;
	}
	/**
	 * 上传检测报告的pdf和图片文件
	 * @param bytes
	 * @param fileName
	 * @return
	 */
	public static String uploadDetectPdfFile(byte[] bytes, String fileName) {
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		String upToken = auth.uploadToken(SPACE_NAME_KEY);
		UploadManager uploadManager = new UploadManager();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String result = "";
		String name=String.valueOf(System.currentTimeMillis());
		try {
			out.write(bytes);
			fileName ="jcbgrz-"+name+"-"+fileName;
			Response res = uploadManager.put(bytes, fileName, upToken);
			if (res.isOK()) {
				result = "http://" + DOMAIN_NAME_KEY + "/" + fileName;
			}
		} catch (Exception e) {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return result;
	}
	   public static void download(String url){
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		     //调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
		     String downloadRUL = auth.privateDownloadUrl(url,3600);
		     System.out.println(downloadRUL);
	   }
	
}
